<?php
/**
 * Author: LCY<409028735@qq.com>
 * Time: 2021/11/19 14:42
 * Note:
 */
namespace app\common\model;
use think\Model;

class Category extends Model
{
    protected $auto = ['icon'];

    public function setIconAttr($val)
    {
       return !empty($val) ? $val : '';
    }

    /**
     * 查询某类型下的所有分类
     * @param string $type
     * @return array
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/22 22:53
     * Note:
     */
    public static function getAllCategoryByType($type = '')
    {
        $categorys = self::where('type', $type)->order(['sort' => 'asc', 'id' => 'asc'])->select();
        if(!$categorys) {
            return [];
        }
        return $categorys->toArray();
    }

    /**
     * 获取某个分类的类型
     * @param int $id
     * @return string
     * Author: LCY<409028735@qq.com>
     * Time: 2021/12/23 13:15
     * Note:
     */
    public static function getTypeById($id = 0)
    {
        $type = '';
        $data = self::get($id);
        if($data && !empty($data['type'])) {
            $type = $data['type'];
        }

        return $type;
    }

}